<template>
  <div class="login">
    <button @click="proFun">测试跨域</button>
    <div class="box">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="ruleForm.userName" clearable></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="passWord" >
          <el-input v-model="ruleForm.passWord" show-password clearable></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >登录</el-button
          >
          <el-button type="primary" @click="register()"
            >注册</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        userName: "",
        passWord: "",
      },
      rules: {
        userName: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          //   { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        passWord: [
          { required: true, message: "请输入密码", trigger: "blur" },
          //   { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    proFun(){
      this.$axios.get('/stage-api/website/coo/list').then((res)=>{
        console.log(res)
      })
    },
    //跳转到注册页面
    register(){
        this.$router.push('/register')
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
        //   alert("submit!");
          this.$axios({
            method: "post",
            url: "/login",
            data: {
              userName: this.ruleForm.userName,
              passWord: this.ruleForm.passWord,
            },
          }).then((res)=>{
            console.log(res.data.code)
            if(res.data.code==200){
                localStorage.setItem("token",res.data.data.token)
                this.$router.replace('/home')
            }
          })
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style lang="scss" scoped>
.login ::v-deep {
  width: 100vw;
  height: 100vh;
  background-color: skyblue;
  display: flex;
  justify-content: center;
  align-items: center;
  background-image: url("@/assets/91ae6bf6b33172100da12d248977d43d1422753272.jpg");
  background-size: contain;
  .box {
    width: 500px;
    height: 300px;
    background-color: rgba(40, 15, 15, 0.342);
    display: flex;
    justify-content: center;
    align-items: center;
    .el-form-item__label {
      width: 80px !important;
    }
    .el-form-item:nth-child(3) {
      .el-form-item__content {
        margin-left: 6px !important;
      }
    }
  }
}
</style>